package com.wuyou.simple;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2283. 判断一个数的数字计数是否等于数位的值
 */
public class DigitCount {

    public static void main(String[] args) {
        String num = "1210";
        System.out.println(solution_2(num));
    }


    public static boolean solution_2(String num){
        char[] chars = num.toCharArray();
        byte[] b = new byte[10];
        for (int i = 0; i < chars.length; i++) {
            if(b[i] > 0) continue;
            int count = 0;
            for (int j = 0; j < chars.length; j++) {
                if(i == chars[j] - 48) count ++;
            }
            if(count != chars[i] - 48) return false;
            b[i] = (byte) count;
        }
        return true;
    }

    public static boolean solution_1(String num){
        char[] chars = num.toCharArray();
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < chars.length; i++) {
            Integer tempCount = map.getOrDefault(chars[i] - 48, 0) + 1;
            map.put(chars[i] - 48, tempCount);
        }
        for (int i = 0; i < chars.length; i++) {
            if(map.getOrDefault(i, 0) != chars[i] - 48) return false;
        }
        return true;
    }
}
